package tech.aistar.corejava.day10.stringextends;

/**
 * Created by 李雨昂 on 2019/7/26.
 */
public class StringCompareDemo {
    public static void main(String[] args) {
        String s2 = "abc";
        //创建了几个对象 - 1个或者2个
        String s1 = new String("abc");//s1指向的是堆非串池里的"abc"

        String s3 = "a" + "b" + "c";//创建了几个对象 "a" "b" "ab" "c" "abc"五个(常量池中没有的情况下)

        System.out.println(s1 == s2);//false
        System.out.println(s3 == s2);//true

        //比较
        //在串池 - 常量池
        String x1 = "a";

        //x2  -> 指向的堆
        String x2 = x1 + "b";//JDK8.0 会进行一些优化  -> newStringBuilder(x); n.append("b")

        String x3 = "ab";

        System.out.println("ab" == x2);//false
        System.out.println(x2 == x3);//false

        //final修饰的字符串肯定在编译期间被JVM放到常量池里
        final String x4 = "a";

        //当x4如果被final修饰那么x4 + "b"  -  JVM在编译期间在底层就会把这句话优化成"a" + "b"
        String x5 = x4 + "b";

        System.out.println(x5 ==x3);//true

        System.out.println("==========");

        String x6 = new String("abc");

        //返回池中的字符串
        String x7 = x6.intern();

        System.out.println("abc" == x7);//true
    }
}
